<?php

namespace Home\Controller;

use Think\Controller;

class LiuyanbanController extends VController
{
    //留言板列表
    public function index()
    {
        $list = M('message')->field('qm.id,qm.uid,qm.content,is_private,qm.ctime,qu.username,qu.zonedp,qm.who_id')->table('qzone_message qm,qzone_user qu')->where("who_id={$this->userData['id']} AND qu.id = qm.uid")->order('ctime desc')->select();
        $master_comment = M('User')->field('master_comment')->find($this->userData['id']);
        $message = D("Message");
        $messageData = $message->field('qm.*,qu.zonedp,qu.username')->table('qzone_user qu,qzone_message qm')->relation(true)->order("ctime DESC")->where("who_id={$this->userData['id']} AND qm.uid=qu.id")->select();
        $this->assign('list', $list);
        $this->assign('messageData', $messageData);
        $this->assign('myUserData', $this->myUserData);
        $this->assign('userData', $this->userData);
        $this->assign('isMy', $this->isMy);
        $this->assign('master_comment',$master_comment['master_comment']);
        $this->display();
    }

    public function add()
    {
        $data = I('POST.');
        $data['uid'] = $this->myUserData['id'];
        $data['ctime'] = time();
        $Model = M('Message');
        $id = $Model->add($data);
        if ($id > 0) {
            $zonedp = $this->myUserData['zonedp'] == '' ? '/images/default.jpg' : $this->myUserData['zonedp'];
            $this->ajaxReturn(array('status' => 0, 'username' => $this->myUserData['username'], 'zonedp' => $zonedp));
        } else {
            $this->ajaxReturn(array('status' => 1));
        }
    }

    public function tosave()
    {
        if (IS_POST) {
            $data = I('post.');
            $data['uid'] = $_SESSION['qzone']['id'];
            $data['ctime'] = time();

            $model = M('message');
            $model->create();

            if (!empty($data['id'])) {
                unset($data['ctime']);
                $res = $model->save($data);
                $id = $data['id'];
            } else {
                $res = $model->add($data);
                $id = $res;
            }

            if ($res) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        } else {
            $this->error('非法请求');
        }
    }

    //删除留言
    public function del()
    {
        $message_id = I('post.message_id');
        $message = M('Message');
        $model = $message->field('uid,who_id')->find($message_id);
        //楼主或者层主都可以删除留言
        if($this->myUserData['id'] == $model['uid'] || $this->myUserData['id'] == $model['who_id'])
        {
            //先把所有的回复删除掉
            M('Message_reply')->where("message_id='{$message_id}'")->delete();
            if($message->delete($message_id)>0)
            {
                //删除掉留言之后，还需要把这条留言所对应的所有回复全删除
                $this->ajaxReturn(array('status'=>0));
            }
            $this->ajaxReturn(array('status'=>1));
        }else
        {
            $this->ajaxReturn(array('status'=>1));
        }
    }
    //回复留言
    public function reply()
    {
        $data = I('post.');
        //检查当前用户是否有权限给这条留言回复，只有楼主或者层主才有回复的权限
        $model = M('Message')->field('uid,who_id')->find($data['message_id']);
        if($this->myUserData['id'] == $model['uid'] || $this->myUserData['id'] == $model['who_id'])
        {
            $data['uid'] = $this->myUserData['id'];
            $data['ctime'] = time();
            $data['username'] = $this->myUserData['username'];
            $data['zonedp'] = $this->myUserData['zonedp'];
            $result = M('MessageReply')->add($data);
            if($result > 0)
            {
                $this->ajaxReturn(array('status'=>0,'message'=>'留言成功'));
            }else
            {
                $this->ajaxReturn(array('status'=>1,'message'=>'留言失败'));
            }
        }else
        {
            $this->ajaxReturn(array('status'=>1,'message'=>'非法来源'));
        }
    }
}